﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Quality_Calculator.DataLoader;
using Quality_Calculator.Skills;
using Quality_Calculator.Skills.Alchemist;
using Quality_Calculator.Skills.Armorer;
using Quality_Calculator.Skills.Carpenter;
using Quality_Calculator.Skills.Generics;
using Quality_Calculator.Skills.Goldsmith;

namespace Quality_Calculator
{
    public partial class MainForm : Form
    {
        private List<SkillBase> Sequence;
        public MainForm()
        {
            Sequence = new List<SkillBase>();
            InitializeComponent();
            ddlCondition.SelectedIndex = 0;
            ddlItemType.SelectedIndex = 0;
            ddlTouch.SelectedIndex = 0;
            toolTip1.SetToolTip(btnBasicSynth, "Basic Synthesis" + Environment.NewLine + "Increases progress" + Environment.NewLine + "Efficiency: 100%" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnStandardSynth, "Standard Synthesis" + Environment.NewLine + "CP: 15" + Environment.NewLine + "Increases progress" + Environment.NewLine + "Efficiency: 150%" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnBasicTouch, "Basic Touch" + Environment.NewLine + "CP: 18" + Environment.NewLine + "Increases quality" + Environment.NewLine + "Efficiency: 100%" + Environment.NewLine + "Success Rate: 70%");
            toolTip1.SetToolTip(btnStandardTouch, "Standard Touch" + Environment.NewLine + "CP: 32" + Environment.NewLine + "Increases quality" + Environment.NewLine + "Efficiency: 125%" + Environment.NewLine + "Success Rate: 80%");
            toolTip1.SetToolTip(btnAdvancedTouch, "Advanced Touch" + Environment.NewLine + "CP: 48" + Environment.NewLine + "Increases quality" + Environment.NewLine + "Efficiency: 150%" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnMastersMend, "Master's Mend" + Environment.NewLine + "CP: 92" + Environment.NewLine + "Restores item durability by 30");
            toolTip1.SetToolTip(btnMastersMend2, "Master's Mend II" + Environment.NewLine + "CP: 160" + Environment.NewLine + "Restores item durability by 60");
            toolTip1.SetToolTip(btnSteadyHand, "Steady Hand" + Environment.NewLine + "CP: 22" + Environment.NewLine + "Improves action success rate by" + Environment.NewLine + "20% for the next five steps");
            toolTip1.SetToolTip(btnInnerQuiet, "Inner Quiet" + Environment.NewLine + "CP: 18" + Environment.NewLine + "Grants a bonus to control with" + Environment.NewLine + "every increase in quality");
            toolTip1.SetToolTip(btnObserve, "Observe" + Environment.NewLine + "CP: 14" + Environment.NewLine + "Do nothing for one step");
            toolTip1.SetToolTip(btnGreatStrides, "Great Strides" + Environment.NewLine + "CP: 32" + Environment.NewLine + "Doubles efficiency of next" + Environment.NewLine + "touch action. Effect active for" + Environment.NewLine + "three steps");
            toolTip1.SetToolTip(btnRapidSynth, "Rapid Synthesis" + Environment.NewLine + "Increases progress" + Environment.NewLine + "Efficiency: 250%" + Environment.NewLine + "Success Rate: 50%");
            toolTip1.SetToolTip(btnBrandOfIce, "Brand of Ice" + Environment.NewLine + "CP: 15" + Environment.NewLine + "Increases progress. Progress doubles" + Environment.NewLine + "when recipe affinity is ice" + Environment.NewLine + "Efficiency: 100% (200%)" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnBrandOfFire, "Brand of Fire" + Environment.NewLine + "CP: 15" + Environment.NewLine + "Increases progress. Progress doubles" + Environment.NewLine + "when recipe affinity is fire" + Environment.NewLine + "Efficiency: 100% (200%)" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnBrandOfWind, "Brand of Wind" + Environment.NewLine + "CP: 15" + Environment.NewLine + "Increases progress. Progress doubles" + Environment.NewLine + "when recipe affinity is wind" + Environment.NewLine + "Efficiency: 100% (200%)" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnBrandOfEarth, "Brand of Earth" + Environment.NewLine + "CP: 15" + Environment.NewLine + "Increases progress. Progress doubles" + Environment.NewLine + "when recipe affinity is earth" + Environment.NewLine + "Efficiency: 100% (200%)" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnBrandOfLightning, "Brand of Lightning" + Environment.NewLine + "CP: 15" + Environment.NewLine + "Increases progress. Progress doubles" + Environment.NewLine + "when recipe affinity is wind" + Environment.NewLine + "Efficiency: 100% (200%)" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnBrandOfWater, "Brand of Water" + Environment.NewLine + "CP: 15" + Environment.NewLine + "Increases progress. Progress doubles" + Environment.NewLine + "when recipe affinity is water" + Environment.NewLine + "Efficiency: 100% (200%)" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnPieceByPiece, "Piece by Piece" + Environment.NewLine + "CP: 15" + Environment.NewLine + "Increases remaining progress by 1/3" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnIngenuity, "Ingenuity" + Environment.NewLine + "CP: 24" + Environment.NewLine + "Lowers recipe level to current level" + Environment.NewLine + "for the next five steps");
            toolTip1.SetToolTip(btnIngenuity2, "Ingenuity II" + Environment.NewLine + "CP: 32" + Environment.NewLine + "Lowers recipe level three below" + Environment.NewLine + "current level for the next five steps");
            toolTip1.SetToolTip(btnRumination, "Rumination" + Environment.NewLine + "Removes Inner Quiet effect and restores CP" + Environment.NewLine + "proportional to the number of times" + Environment.NewLine + "control was increased");
            toolTip1.SetToolTip(btnByregotsBlessing, "Byregot's Blessing" + Environment.NewLine + "CP: 24" + Environment.NewLine + "Increases quality" + Environment.NewLine + "Efficiency: 100% plus 20% for each bonus" + Environment.NewLine + "to control granted by Inner Quiet" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnHastyTouch, "Hasty Touch" + Environment.NewLine + "Increases quality" + Environment.NewLine + "Efficiency: 100%" + Environment.NewLine + "Success Rate: 50%");
            toolTip1.SetToolTip(btnSteadyHand2, "Steady Hand II" + Environment.NewLine + "CP: 25" + Environment.NewLine + "Improves action success rate by 30%" + Environment.NewLine + "for the next five steps");
            toolTip1.SetToolTip(btnReclaim, "Reclaim" + Environment.NewLine + "CP: 55" + Environment.NewLine + "Increases the chance materials will not be" + Environment.NewLine + "lost after botched synthesis to 90%");
            toolTip1.SetToolTip(btnManipulation, "Manipulation" + Environment.NewLine + "CP: 88" + Environment.NewLine + "Restores 10 points of durability after" + Environment.NewLine + "each step for the next three steps");
            toolTip1.SetToolTip(btnFlawlessSynth, "Flawless Synthesis" + Environment.NewLine + "CP: 37" + Environment.NewLine + "Increases progress by 40" + Environment.NewLine + "Success Rate: 90%");
            toolTip1.SetToolTip(btnInnovation, "Innovation" + Environment.NewLine + "CP: 18" + Environment.NewLine + "Increases control by 50% for" + Environment.NewLine + "the next three steps");
            toolTip1.SetToolTip(btnWasteNot, "Waste Not" + Environment.NewLine + "CP: 56" + Environment.NewLine + "Reduces loss of durability by 50% for the next four steps");
            toolTip1.SetToolTip(btnWasteNot2, "Waste Not II" + Environment.NewLine + "CP: 98" + Environment.NewLine + "Reduces loss of durability by 50% for the next eight steps");
            toolTip1.SetToolTip(btnCarefulSynth, "Careful Synthesis" + Environment.NewLine + "Increases progress" + Environment.NewLine + "Efficiency: 90%" + Environment.NewLine + "Success Rate: 100%");
            toolTip1.SetToolTip(btnCarefulSynth2, "Careful Synthesis II" + Environment.NewLine + "Increases progress" + Environment.NewLine + "Efficiency: 120%" + Environment.NewLine + "Success Rate: 100%");
            toolTip1.SetToolTip(btnTricksOfTheTrade, "Tricks of the Trade" + Environment.NewLine + "Restores 20 cp. Can only be used" + Environment.NewLine + "when material condition is good");
            toolTip1.SetToolTip(btnComfortZone, "Comfort Zone" + Environment.NewLine + "CP: 66" + Environment.NewLine + "Restores 8 CP after each step" + Environment.NewLine + "for the next ten steps");
            toolTip1.SetToolTip(btnAutoLoad, "Will attempt to load your crafter stats directly from the game" + Environment.NewLine + "Requires the game to be running, and your character on the proper crafter class");

        }




        private void Calculate()
        {
            try
            {
                double BaseControl = int.Parse(tbControl.Text);
                double CLevel = double.Parse(tbCLevel.Text);
                double ILevel = double.Parse(tbILevel.Text);
                double StackCount = (double)nuStacks.Value;
                double isInnov = chInnov.Checked ? 1 : 0;
                double malus = ILevel - CLevel >= 3 ? 1 - (ILevel - CLevel) * 0.05 : 1;
                if (chInge1.Checked || chInge2.Checked) malus = 1;
                double bonus = 1;
                if (ddlCondition.SelectedItem == "Good") bonus = 1.5;
                if (ddlCondition.SelectedItem == "Excellent") bonus = 4;
                if (ddlCondition.SelectedItem == "Poor") bonus = 0.5;
                double efficiency = 1;
                if (ddlTouch.SelectedItem == "Standard Touch(125%)") efficiency = 1.25;
                if (ddlTouch.SelectedItem == "Advanced Touch(150%)") efficiency = 1.5;
                if (ddlTouch.SelectedItem == "Byregot's Blessing(100% +20% per stack)") efficiency = 1 + 0.2 * StackCount;
                double TrueControl = BaseControl + BaseControl * 0.2 * StackCount + BaseControl * 0.5 * isInnov;
                double QualityIncr = 0;

                if(ddlItemType.SelectedIndex == 0)
                    QualityIncr= Formulas.Quality(TrueControl) * bonus * efficiency * malus;
                if (ddlItemType.SelectedIndex == 1)
                    QualityIncr = Formulas.QualityStar1(TrueControl) * bonus * efficiency * malus;
                if(ddlItemType.SelectedIndex == 1 && chInge1.Checked)
                    QualityIncr = Formulas.QualityStar1Inge(TrueControl) * bonus * efficiency * malus;
                if (chStride.Checked) QualityIncr *= 2;
                lbControl.Text = TrueControl.ToString("#.00");
                lbQual.Text = Math.Round(QualityIncr).ToString();
            }
            catch (Exception e)
            {
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            Calculate();
        }
        private void AddIconSequence(Image img)
        {
            PictureBox pictureBox = new PictureBox();
            pictureBox.Image = img;
            pictureBox.Location = new System.Drawing.Point(3, 3);
            pictureBox.Name = "pictureBox1";
            pictureBox.Size = new System.Drawing.Size(42, 42);
            pictureBox.TabIndex = 0;
            pictureBox.TabStop = false;
            flowLayoutPanel1.Controls.Add(pictureBox);
            pictureBox.MouseClick += pictureBox_MouseClick;

            Synthesis synth = new Synthesis()
            {
                StartingQual = int.Parse(tbSQual.Text),
                BaseControl = int.Parse(tbControl1.Text),
                CPMax = int.Parse(tbCp.Text),
                CraftmanShip = int.Parse(tbCraftmanship.Text),
                DurabilityMax = int.Parse(tbDurability.Text),
                ProgressMax = int.Parse(tbDifficulty.Text),
                QualityMax = int.Parse(tbQuality.Text),
                Sequence = Sequence
            };
            int countSim = 1;
            synth.RunSimulation(countSim);
            tbDurabilityLeft.Text = synth.DurabilityCurrent.ToString();
            tbCpLeft2.Text = synth.CPCurrent.ToString();
        }
        private void AddIconSequenceTricks(Image img)
        {
            PictureBox pictureBox = new PictureBox();
            pictureBox.Image = img;
            pictureBox.Location = new System.Drawing.Point(3, 3);
            pictureBox.Name = "pictureBox1";
            pictureBox.Size = new System.Drawing.Size(42, 42);
            pictureBox.TabIndex = 0;
            pictureBox.TabStop = false;
            flowLayoutPanel1.Controls.Add(pictureBox);
            pictureBox.MouseClick += pictureBox2_MouseClick;

            Synthesis synth = new Synthesis()
            {
                StartingQual = int.Parse(tbSQual.Text),
                BaseControl = int.Parse(tbControl1.Text),
                CPMax = int.Parse(tbCp.Text),
                CraftmanShip = int.Parse(tbCraftmanship.Text),
                DurabilityMax = int.Parse(tbDurability.Text),
                ProgressMax = int.Parse(tbDifficulty.Text),
                QualityMax = int.Parse(tbQuality.Text),
                Sequence = Sequence
            };
            int countSim = 1;
            synth.RunSimulation(countSim);
            tbDurabilityLeft.Text = synth.DurabilityCurrent.ToString();
            tbCpLeft2.Text = synth.CPCurrent.ToString();
        }
        void pictureBox2_MouseClick(object sender, MouseEventArgs e)
        {
            int index = flowLayoutPanel1.Controls.IndexOf((Control)sender);
            TricksForm tf = new TricksForm(((BuffSkillBase)Sequence[index]).Duration);
            if (tf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (tf.tbLength.Text != "-1")
                {
                    ((BuffSkillBase)Sequence[index]).Duration = int.Parse(tf.tbLength.Text);
                }
                else
                {
                    Sequence.RemoveAt(index);
                    flowLayoutPanel1.Controls.Remove((Control)sender);
                }
            }
        }
        void pictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            Sequence.RemoveAt(flowLayoutPanel1.Controls.IndexOf((Control)sender));
            flowLayoutPanel1.Controls.Remove((Control)sender);
        }
        private void btnBasicSynth_Click(object sender, EventArgs e)
        {
            Sequence.Add(new SynthSkill() { Name = "Basic Synthesis", DurabilityCost = 10, Chance = 90, Efficiency = 1.0, Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnStandardSynth_Click(object sender, EventArgs e)
        {
            Sequence.Add(new SynthSkill() { Name = "Standard Synthesis", DurabilityCost = 10, Chance = 90, Efficiency = 1.5,CpCost = 15, Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnBasicTouch_Click(object sender, EventArgs e)
        {
            Sequence.Add(new TouchSkill() { Name = "Basic Touch", DurabilityCost = 10, Chance = 70, Efficiency = 1.0,CpCost = 18, Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnStandardTouch_Click(object sender, EventArgs e)
        {
            Sequence.Add(new TouchSkill() { Name = "Standard Touch", DurabilityCost = 10, Chance = 80, Efficiency = 1.2, CpCost = 32, Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnAdvancedTouch_Click(object sender, EventArgs e)
        {
            Sequence.Add(new TouchSkill() { Name = "Advanced Touch", DurabilityCost = 10, Chance = 90, Efficiency = 1.5, CpCost = 48, Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnMastersMend_Click(object sender, EventArgs e)
        {
            Sequence.Add(new MastersMend());
            AddIconSequence(((Button)sender).Image);
        }

        private void btnMastersMend2_Click(object sender, EventArgs e)
        {
            Sequence.Add(new MastersMend2());
            AddIconSequence(((Button)sender).Image);
        }

        private void btnSteadyHand_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 22, Duration = 5, Enum = eSpell.SteadyHands, Name = "Steady Hand", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnInnerQuiet_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 18, Duration = -1, Enum = eSpell.InnerQuiet, Name = "Inner Quiet", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnGreatStrides_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 32, Duration = 3, Enum = eSpell.GreatStrides, Name = "Great Strides", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnObserve_Click(object sender, EventArgs e)
        {
            Sequence.Add(new Observe());
            AddIconSequence(((Button)sender).Image);
        }

        private void btnTricksOfTheTrade_Click(object sender, EventArgs e)
        {
            TricksForm tf = new TricksForm(2);
            if (tf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Sequence.Add(new TricksOfTheTrade(int.Parse(tf.tbLength.Text)));
                AddIconSequenceTricks(((Button)sender).Image);
            }
        }

        private void btnComfortZone_Click(object sender, EventArgs e)
        {
            Sequence.Add(new ComfortZone());
            AddIconSequence(((Button)sender).Image);
        }

        private void btnManipulation_Click(object sender, EventArgs e)
        {
            Sequence.Add(new Manipulation());
            AddIconSequence(((Button)sender).Image);
        }

        private void btnFlawlessSynth_Click(object sender, EventArgs e)
        {
            Sequence.Add(new FlawlessSynthesis());
            AddIconSequence(((Button)sender).Image);
        }

        private void btnInnovation_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 18, Duration = 3, Enum = eSpell.Innovation, Name = "Innovation", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnIngenuity_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 24, Duration = 5, Enum = eSpell.Ingenuity, Name = "Ingenuity", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnIngenuity2_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 32, Duration = 5, Enum = eSpell.Ingenuity2, Name = "Ingenuity II", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnCarefulSynth_Click(object sender, EventArgs e)
        {
            Sequence.Add(new SynthSkill() { Name = "Careful Synthesis", DurabilityCost = 10, Chance = 100, Efficiency = 0.9, CpCost = 0, Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnCarefulSynth2_Click(object sender, EventArgs e)
        {
            Sequence.Add(new SynthSkill() { Name = "Careful Synthesis II", DurabilityCost = 10, Chance = 100, Efficiency = 1.2, CpCost = 0, Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnRapidSynth_Click(object sender, EventArgs e)
        {
            Sequence.Add(new SynthSkill() { Name = "Rapid Synthesis", DurabilityCost = 10, Chance = 50, Efficiency = 2.5, CpCost = 0, Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnPieceByPiece_Click(object sender, EventArgs e)
        {
            Sequence.Add(new PieceByPiece());
            AddIconSequence(((Button)sender).Image);
        }

        private void btnHastyTouch_Click(object sender, EventArgs e)
        {
            Sequence.Add(new TouchSkill() { Name = "Hasty Touch", DurabilityCost = 10, Chance = 50, Efficiency = 1.0, CpCost = 0, Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnSteadyHand2_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 25, Duration = 5, Enum = eSpell.SteadyHands2, Name = "Steady Hand II", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnReclaim_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 55, Duration = -1, Enum = eSpell.Reclaim, Name = "Reclaim", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnRumination_Click(object sender, EventArgs e)
        {
            Sequence.Add(new Rumination());
            AddIconSequence(((Button)sender).Image);
        }

        private void btnByregotsBlessing_Click(object sender, EventArgs e)
        {
            Sequence.Add(new ByregotsBlessing());
            AddIconSequence(((Button)sender).Image);
        }

        private void btnWasteNot_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 56, Duration = 4, Enum = eSpell.WasteNot, Name = "Waste Not", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void btnWasteNot2_Click(object sender, EventArgs e)
        {
            Sequence.Add(new BuffSkillBase() { CpCost = 98, Duration = 8, Enum = eSpell.WasteNot2, Name = "Waste Not II", Icon = ((Button)sender).Image });
            AddIconSequence(((Button)sender).Image);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Synthesis synth = new Synthesis()
            {
                StartingQual = int.Parse(tbSQual.Text),
                BaseControl = int.Parse(tbControl1.Text),
                CPMax = int.Parse(tbCp.Text),
                CraftmanShip = int.Parse(tbCraftmanship.Text),
                DurabilityMax = int.Parse(tbDurability.Text),
                ProgressMax = int.Parse(tbDifficulty.Text),
                QualityMax = int.Parse(tbQuality.Text),
                Sequence = Sequence
            };
            int countSim = 1000;
            synth.RunSimulation(countSim);
            double HQP = ((double)synth.HQcount /(double)countSim) * 100;
            double FailP = ((double)synth.FailCount /(double) countSim) * 100;
            double CPLeft = ((double)synth.CPFinalCounter / synth.Simulations);
            tbHQP.Text = HQP.ToString("#.00");
            tbFailP.Text = FailP.ToString("#.00");
            tbCPLeft.Text = CPLeft.ToString("#.00"); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CrafterData stats = StatLoader.LoadStats();
            tbControl1.Text = stats.Control.ToString();
            tbCraftmanship.Text = stats.Craftmanship.ToString();
            tbCp.Text = stats.CP.ToString();
        }
    }
}
